global n k casenum kmax draws
% This file conducts the GMM IV-Logit estimation on turnout for the PRI,
% and the major opposition taking abstention and votes for the remaining
% parties as baseline doing FGLS with robust COVAR matrix
% Tobias Pfutze, May 2009

% Upload Data in order: pripart oppmaxpart remit_prophh_intl
% dist_border cycleB1 cycleB2 zona_norte labinc_perhh_log
% labinc_perhh_stdev illiterate indig margmuni00 poptot_2000 unempl
% raildistjuarez_05 histmig24_edoprop00 oppwin_since80 pripart1 oppmaxpart1

RAW=load('/Users/tpfutze/Dropbox/Research/Participation/Matlab/Data/participation_all.txt');
[N,M]=size(RAW);

% Replace total population by its logged value
RAW(:,13)=log(RAW(:,13));
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start by creating single random vector for bootstrap %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
draws=10000;
rand('state',1812);
unifrv=rand(draws,1);

% Create two separate matrices conditional on whether municipality has 
% had an opposition party in office since 1980
s0=1;
s1=1;
for i=1:N
    if (RAW(i,17)==0)
        RAW0(s0,1:M)=RAW(i,1:M);
        s0=s0+1;
    elseif RAW(i,17)==1
        RAW1(s1,1:M)=RAW(i,1:M);
        s1=s1+1;
    end
end
[N0,M0]=size(RAW0);
[N1,M1]=size(RAW1);


% Create vectors with randomly selected observations for bootstrap
RAND=ceil(N*unifrv);
RAND0=ceil(N0*unifrv);
RAND1=ceil(N1*unifrv);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create all matrices of dep., indep. and instr. variables %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create matrices and vectors 

%Dependent variable are log odds ratios
y=log(RAW(:,1:2)./(kron(ones(1,2),(1-RAW(:,1)-RAW(:,2)))));
y0=log(RAW0(:,1:2)./(kron(ones(1,2),(1-RAW0(:,1)-RAW0(:,2)))));       
y1=log(RAW1(:,1:2)./(kron(ones(1,2),(1-RAW1(:,1)-RAW1(:,2)))));

x1=[ones(N,1) RAW(:,3:7)];                               
x2=[ones(N,1) RAW(:,3:14)];  

x01=[ones(N0,1) RAW0(:,3:7)];                               
x02=[ones(N0,1) RAW0(:,3:14)];  

x11=[ones(N1,1) RAW1(:,3:7)];                               
x12=[ones(N1,1) RAW1(:,3:14)];  

z1=[ones(N,1) RAW(:,15:16) RAW(:,4:7)];                               
z2=[ones(N,1) RAW(:,15:16) RAW(:,4:14)]; 

z01=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:7)];                               
z02=[ones(N0,1) RAW0(:,15:16) RAW0(:,4:14)];  

z11=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:7)];                               
z12=[ones(N1,1) RAW1(:,15:16) RAW1(:,4:14)];  


%%%%%%%%%%%%%%%%%%%
% DEFINE MATRICES %
%%%%%%%%%%%%%%%%%%%

MEAN1=[mean(x1(:,1:3)) 0 0 0];
MEAN2=[mean(x2(:,1:3)) 0 0 0  mean(x2(:,7:13))];

%Matrices for point estimates and significance tests
[nmax,kmax]=size(x2);

BETA=zeros(2*kmax,6);    % betas for shares
F_WALD=zeros(3,6);
T_WALD=zeros(2*kmax,6);
TJ_WALD=zeros(kmax,6);
PARTIAL=zeros(2*(kmax-1),6);
DOF=zeros(2,6);
OIR=zeros(1,6);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%% PARTICIPATION RESULTS %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ENTIRE SAMPLE %%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=1;
[n,k] = size(x1);
[n,l] = size(z1);
mean = MEAN1;
Y=(reshape(y',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x1(i,:));
    Z((i-1)*2+1:(i-1)*2+2,1:2*l)=kron(eye(2,2),z1(i,:));
end


[beta, T, T_J, F, part, oir] = SURPartiesRobust_IVGMM(Y, X, Z, mean, RAND, n, k, l);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
DOF(:,casenum)=[k, l];
OIR(:, casenum)=oir;
clear Y X Z P beta  W F T T_J alpha ETA part mean mean_l mean_h oir;
 


% 2ND CASE
casenum=casenum+1;
[n,k] = size(x2);
[n,l] = size(z2);
mean = MEAN2;
Y=(reshape(y',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x2(i,:));
    Z((i-1)*2+1:(i-1)*2+2,1:2*l)=kron(eye(2,2),z2(i,:));
end


[beta, T, T_J, F, part, oir] = SURPartiesRobust_IVGMM(Y, X, Z, mean, RAND, n, k, l);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
DOF(:,casenum)=[k, l];
OIR(:, casenum)=oir;
clear Y X Z P beta  W F T T_J alpha ETA part mean mean_l mean_h oir;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%   ALWAYS PRI   %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=casenum+1;
[n,k] = size(x01);
[n,l] = size(z01);
mean = MEAN1;
Y=(reshape(y0',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x01(i,:));
    Z((i-1)*2+1:(i-1)*2+2,1:2*l)=kron(eye(2,2),z01(i,:));
end

[beta, T, T_J, F, part, oir] = SURPartiesRobust_IVGMM(Y, X, Z, mean, RAND0, n, k, l);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
DOF(:,casenum)=[k, l];
OIR(:, casenum)=oir;
clear Y X Z P beta  W F T T_J alpha ETA part mean mean_l mean_h oir;
 


% 2ND CASE
casenum=casenum+1;
[n,k] = size(x02);
[n,l] = size(z02);
mean = MEAN2;
Y=(reshape(y0',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x02(i,:));
    Z((i-1)*2+1:(i-1)*2+2,1:2*l)=kron(eye(2,2),z02(i,:));
end


[beta, T, T_J, F, part, oir] = SURPartiesRobust_IVGMM(Y, X, Z, mean, RAND0, n, k, l);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
DOF(:,casenum)=[k, l];
OIR(:, casenum)=oir;
clear Y X Z P beta  W F T T_J alpha ETA part mean mean_l mean_h oir;




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%% ALREADY OPPOSITION %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1ST CASE
casenum=casenum+1;
[n,k] = size(x11);
[n,l] = size(z11);
mean = MEAN1;
Y=(reshape(y1',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x11(i,:));
    Z((i-1)*2+1:(i-1)*2+2,1:2*l)=kron(eye(2,2),z11(i,:));
end


[beta, T, T_J, F, part, oir] = SURPartiesRobust_IVGMM(Y, X, Z, mean, RAND1, n, k, l);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
DOF(:,casenum)=[k, l];
OIR(:, casenum)=oir;
clear Y X Z P beta  W F T T_J alpha ETA part mean mean_l mean_h oir;
 


% 2ND CASE
casenum=casenum+1;
[n,k] = size(x12);
[n,l] = size(z12);
mean = MEAN2;
Y=(reshape(y1',1,2*n))';
for i=1:n
    X((i-1)*2+1:(i-1)*2+2,1:2*k)=kron(eye(2,2),x12(i,:));
    Z((i-1)*2+1:(i-1)*2+2,1:2*l)=kron(eye(2,2),z12(i,:));
end


[beta, T, T_J, F, part, oir] = SURPartiesRobust_IVGMM(Y, X, Z, mean, RAND1, n, k, l);


% Put into final matrices
BETA(1:k, casenum)=beta(1:k);
BETA(kmax+1:kmax+k, casenum)=beta(k+1:2*k);
T_WALD(1:k, casenum)=T(1:k);
T_WALD(kmax+1:kmax+k, casenum)=T(k+1:2*k);
TJ_WALD(1:k, casenum)=T_J(1:k);
F_WALD(:, casenum)=F;
PARTIAL(1:k-1, casenum)=part(1:k-1);
PARTIAL(kmax:kmax+(k-2), casenum)=part(k:2*k-2);
DOF(:,casenum)=[k, l];
OIR(:, casenum)=oir;
clear Y X Z P beta  W F T T_J alpha ETA part mean mean_l mean_h oir;






%%%%%%%%%%%%%%%%%%%
% Compute p-values%
%%%%%%%%%%%%%%%%%%%

PVAL_TWALD=1-chi2cdf(T_WALD.^2,1);
PVAL_TJWALD=1-chi2cdf(TJ_WALD,3);
FDOF_ALL=kron([2; 1; 1],DOF(1,:));
PVAL_FWALD=1-chi2cdf(F_WALD,FDOF_ALL);
PVAL_OIR=1-chi2cdf(OIR,DOF(2,:)-DOF(1,:));






save ParticipPartiesMaxRobust_IVGMMResults BETA PARTIAL PVAL_TWALD PVAL_TJWALD PVAL_FWALD T_WALD TJ_WALD F_WALD PVAL_OIR OIR;


